﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using VCS.Entities;
using VCS.Data;

#endregion

namespace VCS.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ProcedureDiscountProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ProcedureDiscountProviderBaseCore : EntityProviderBase<VCS.Entities.ProcedureDiscount, VCS.Entities.ProcedureDiscountKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, VCS.Entities.ProcedureDiscountKey key)
		{
			return Delete(transactionManager, key.MemberType, key.ProcedureCode);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_memberType">. Primary Key.</param>
		/// <param name="_procedureCode">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _memberType, System.String _procedureCode)
		{
			return Delete(null, _memberType, _procedureCode);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType">. Primary Key.</param>
		/// <param name="_procedureCode">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _memberType, System.String _procedureCode);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		FK_ProcedureDiscount_MemberType Description: 
		/// </summary>
		/// <param name="_memberType"></param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByMemberType(System.String _memberType)
		{
			int count = -1;
			return GetByMemberType(_memberType, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		FK_ProcedureDiscount_MemberType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		/// <remarks></remarks>
		public TList<ProcedureDiscount> GetByMemberType(TransactionManager transactionManager, System.String _memberType)
		{
			int count = -1;
			return GetByMemberType(transactionManager, _memberType, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		FK_ProcedureDiscount_MemberType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByMemberType(TransactionManager transactionManager, System.String _memberType, int start, int pageLength)
		{
			int count = -1;
			return GetByMemberType(transactionManager, _memberType, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		fkProcedureDiscountMemberType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_memberType"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByMemberType(System.String _memberType, int start, int pageLength)
		{
			int count =  -1;
			return GetByMemberType(null, _memberType, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		fkProcedureDiscountMemberType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_memberType"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByMemberType(System.String _memberType, int start, int pageLength,out int count)
		{
			return GetByMemberType(null, _memberType, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_MemberType key.
		///		FK_ProcedureDiscount_MemberType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public abstract TList<ProcedureDiscount> GetByMemberType(TransactionManager transactionManager, System.String _memberType, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		FK_ProcedureDiscount_Procedure Description: 
		/// </summary>
		/// <param name="_procedureCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByProcedureCode(System.String _procedureCode)
		{
			int count = -1;
			return GetByProcedureCode(_procedureCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		FK_ProcedureDiscount_Procedure Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		/// <remarks></remarks>
		public TList<ProcedureDiscount> GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode)
		{
			int count = -1;
			return GetByProcedureCode(transactionManager, _procedureCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		FK_ProcedureDiscount_Procedure Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode, int start, int pageLength)
		{
			int count = -1;
			return GetByProcedureCode(transactionManager, _procedureCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		fkProcedureDiscountProcedure Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_procedureCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByProcedureCode(System.String _procedureCode, int start, int pageLength)
		{
			int count =  -1;
			return GetByProcedureCode(null, _procedureCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		fkProcedureDiscountProcedure Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_procedureCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public TList<ProcedureDiscount> GetByProcedureCode(System.String _procedureCode, int start, int pageLength,out int count)
		{
			return GetByProcedureCode(null, _procedureCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ProcedureDiscount_Procedure key.
		///		FK_ProcedureDiscount_Procedure Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.ProcedureDiscount objects.</returns>
		public abstract TList<ProcedureDiscount> GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override VCS.Entities.ProcedureDiscount Get(TransactionManager transactionManager, VCS.Entities.ProcedureDiscountKey key, int start, int pageLength)
		{
			return GetByMemberTypeProcedureCode(transactionManager, key.MemberType, key.ProcedureCode, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(System.String _memberType, System.String _procedureCode)
		{
			int count = -1;
			return GetByMemberTypeProcedureCode(null,_memberType, _procedureCode, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(System.String _memberType, System.String _procedureCode, int start, int pageLength)
		{
			int count = -1;
			return GetByMemberTypeProcedureCode(null, _memberType, _procedureCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(TransactionManager transactionManager, System.String _memberType, System.String _procedureCode)
		{
			int count = -1;
			return GetByMemberTypeProcedureCode(transactionManager, _memberType, _procedureCode, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(TransactionManager transactionManager, System.String _memberType, System.String _procedureCode, int start, int pageLength)
		{
			int count = -1;
			return GetByMemberTypeProcedureCode(transactionManager, _memberType, _procedureCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(System.String _memberType, System.String _procedureCode, int start, int pageLength, out int count)
		{
			return GetByMemberTypeProcedureCode(null, _memberType, _procedureCode, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ProcedureDiscount index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.ProcedureDiscount"/> class.</returns>
		public abstract VCS.Entities.ProcedureDiscount GetByMemberTypeProcedureCode(TransactionManager transactionManager, System.String _memberType, System.String _procedureCode, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;ProcedureDiscount&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;ProcedureDiscount&gt;"/></returns>
		public static TList<ProcedureDiscount> Fill(IDataReader reader, TList<ProcedureDiscount> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				VCS.Entities.ProcedureDiscount c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("ProcedureDiscount")
					.Append("|").Append((System.String)reader[((int)ProcedureDiscountColumn.MemberType - 1)])
					.Append("|").Append((System.String)reader[((int)ProcedureDiscountColumn.ProcedureCode - 1)]).ToString();
					c = EntityManager.LocateOrCreate<ProcedureDiscount>(
					key.ToString(), // EntityTrackingKey
					"ProcedureDiscount",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new VCS.Entities.ProcedureDiscount();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.MemberType = (System.String)reader[((int)ProcedureDiscountColumn.MemberType - 1)];
					c.OriginalMemberType = c.MemberType;
					c.ProcedureCode = (System.String)reader[((int)ProcedureDiscountColumn.ProcedureCode - 1)];
					c.OriginalProcedureCode = c.ProcedureCode;
					c.DiscountPercent = (System.Int32)reader[((int)ProcedureDiscountColumn.DiscountPercent - 1)];
					c.Remark = (reader.IsDBNull(((int)ProcedureDiscountColumn.Remark - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.Remark - 1)];
					c.UpdateUser = (reader.IsDBNull(((int)ProcedureDiscountColumn.UpdateUser - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.UpdateUser - 1)];
					c.UpdateDate = (System.DateTime)reader[((int)ProcedureDiscountColumn.UpdateDate - 1)];
					c.CreateUser = (reader.IsDBNull(((int)ProcedureDiscountColumn.CreateUser - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.CreateUser - 1)];
					c.CreateDate = (System.DateTime)reader[((int)ProcedureDiscountColumn.CreateDate - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.ProcedureDiscount"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.ProcedureDiscount"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, VCS.Entities.ProcedureDiscount entity)
		{
			if (!reader.Read()) return;
			
			entity.MemberType = (System.String)reader[((int)ProcedureDiscountColumn.MemberType - 1)];
			entity.OriginalMemberType = (System.String)reader["MemberType"];
			entity.ProcedureCode = (System.String)reader[((int)ProcedureDiscountColumn.ProcedureCode - 1)];
			entity.OriginalProcedureCode = (System.String)reader["ProcedureCode"];
			entity.DiscountPercent = (System.Int32)reader[((int)ProcedureDiscountColumn.DiscountPercent - 1)];
			entity.Remark = (reader.IsDBNull(((int)ProcedureDiscountColumn.Remark - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.Remark - 1)];
			entity.UpdateUser = (reader.IsDBNull(((int)ProcedureDiscountColumn.UpdateUser - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.UpdateUser - 1)];
			entity.UpdateDate = (System.DateTime)reader[((int)ProcedureDiscountColumn.UpdateDate - 1)];
			entity.CreateUser = (reader.IsDBNull(((int)ProcedureDiscountColumn.CreateUser - 1)))?null:(System.String)reader[((int)ProcedureDiscountColumn.CreateUser - 1)];
			entity.CreateDate = (System.DateTime)reader[((int)ProcedureDiscountColumn.CreateDate - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.ProcedureDiscount"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.ProcedureDiscount"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, VCS.Entities.ProcedureDiscount entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.MemberType = (System.String)dataRow["MemberType"];
			entity.OriginalMemberType = (System.String)dataRow["MemberType"];
			entity.ProcedureCode = (System.String)dataRow["ProcedureCode"];
			entity.OriginalProcedureCode = (System.String)dataRow["ProcedureCode"];
			entity.DiscountPercent = (System.Int32)dataRow["DiscountPercent"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.UpdateUser = Convert.IsDBNull(dataRow["UpdateUser"]) ? null : (System.String)dataRow["UpdateUser"];
			entity.UpdateDate = (System.DateTime)dataRow["UpdateDate"];
			entity.CreateUser = Convert.IsDBNull(dataRow["CreateUser"]) ? null : (System.String)dataRow["CreateUser"];
			entity.CreateDate = (System.DateTime)dataRow["CreateDate"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="VCS.Entities.ProcedureDiscount"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">VCS.Entities.ProcedureDiscount Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, VCS.Entities.ProcedureDiscount entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region MemberTypeSource	
			if (CanDeepLoad(entity, "MemberType|MemberTypeSource", deepLoadType, innerList) 
				&& entity.MemberTypeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.MemberType;
				MemberType tmpEntity = EntityManager.LocateEntity<MemberType>(EntityLocator.ConstructKeyFromPkItems(typeof(MemberType), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.MemberTypeSource = tmpEntity;
				else
					entity.MemberTypeSource = DataRepository.MemberTypeProvider.GetByMemberType(transactionManager, entity.MemberType);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MemberTypeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MemberTypeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.MemberTypeProvider.DeepLoad(transactionManager, entity.MemberTypeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion MemberTypeSource

			#region ProcedureCodeSource	
			if (CanDeepLoad(entity, "MedicalProcedure|ProcedureCodeSource", deepLoadType, innerList) 
				&& entity.ProcedureCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ProcedureCode;
				MedicalProcedure tmpEntity = EntityManager.LocateEntity<MedicalProcedure>(EntityLocator.ConstructKeyFromPkItems(typeof(MedicalProcedure), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ProcedureCodeSource = tmpEntity;
				else
					entity.ProcedureCodeSource = DataRepository.MedicalProcedureProvider.GetByProcedureCode(transactionManager, entity.ProcedureCode);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ProcedureCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ProcedureCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.MedicalProcedureProvider.DeepLoad(transactionManager, entity.ProcedureCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ProcedureCodeSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the VCS.Entities.ProcedureDiscount object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">VCS.Entities.ProcedureDiscount instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">VCS.Entities.ProcedureDiscount Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, VCS.Entities.ProcedureDiscount entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region MemberTypeSource
			if (CanDeepSave(entity, "MemberType|MemberTypeSource", deepSaveType, innerList) 
				&& entity.MemberTypeSource != null)
			{
				DataRepository.MemberTypeProvider.Save(transactionManager, entity.MemberTypeSource);
				entity.MemberType = entity.MemberTypeSource.MemberType;
			}
			#endregion 
			
			#region ProcedureCodeSource
			if (CanDeepSave(entity, "MedicalProcedure|ProcedureCodeSource", deepSaveType, innerList) 
				&& entity.ProcedureCodeSource != null)
			{
				DataRepository.MedicalProcedureProvider.Save(transactionManager, entity.ProcedureCodeSource);
				entity.ProcedureCode = entity.ProcedureCodeSource.ProcedureCode;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ProcedureDiscountChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>VCS.Entities.ProcedureDiscount</c>
	///</summary>
	public enum ProcedureDiscountChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>MemberType</c> at MemberTypeSource
		///</summary>
		[ChildEntityType(typeof(MemberType))]
		MemberType,
			
		///<summary>
		/// Composite Property for <c>MedicalProcedure</c> at ProcedureCodeSource
		///</summary>
		[ChildEntityType(typeof(MedicalProcedure))]
		MedicalProcedure,
		}
	
	#endregion ProcedureDiscountChildEntityTypes
	
	#region ProcedureDiscountFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ProcedureDiscountColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ProcedureDiscount"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ProcedureDiscountFilterBuilder : SqlFilterBuilder<ProcedureDiscountColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountFilterBuilder class.
		/// </summary>
		public ProcedureDiscountFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ProcedureDiscountFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ProcedureDiscountFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ProcedureDiscountFilterBuilder
	
	#region ProcedureDiscountParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ProcedureDiscountColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ProcedureDiscount"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ProcedureDiscountParameterBuilder : ParameterizedSqlFilterBuilder<ProcedureDiscountColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountParameterBuilder class.
		/// </summary>
		public ProcedureDiscountParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ProcedureDiscountParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ProcedureDiscountParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ProcedureDiscountParameterBuilder
	
	#region ProcedureDiscountSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ProcedureDiscountColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ProcedureDiscount"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ProcedureDiscountSortBuilder : SqlSortBuilder<ProcedureDiscountColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ProcedureDiscountSqlSortBuilder class.
		/// </summary>
		public ProcedureDiscountSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ProcedureDiscountSortBuilder
	
} // end namespace
